package main

import (
	"k8s-plantform/config"
	"k8s-plantform/controller"
	"k8s-plantform/db"
	"k8s-plantform/middle"
	"k8s-plantform/service"
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	//初始化数据库
	db.Init()

	//初始化k8s client
	service.K8s.Init() //可以使用service.k8s.clientset

	//初始化gin
	r := gin.Default()

	//加载跨域中间件
	r.Use(middle.Cors())

	//加载jwt中间件
	r.Use(middle.JWTAuth())

	//跨包调用router的初始化方法
	controller.Router.InitApiRouter(r)

	//启动websocket
	//异步执行，所以要放在gin程序(阻塞)启动前
	go func() {
		http.HandleFunc("/ws", service.Terminal.WsHandler)
		http.ListenAndServe(":8081", nil)
	}()

	//启动gin server
	r.Run(config.ListenAddr)

	//关闭数据库连接
	db.Close()
}
